﻿package mortal.game.view.forging.view
{
    import Message.Game.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.forging.*;
    import mortal.game.view.forging.data.*;
    import mortal.game.view.forging.util.*;
    import mortal.game.view.palyer.*;
    import mortal.game.view.shop.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;

    public class GemStrengthenPanel3 extends ForgingPanelBase
    {
        private var _gemInfo1:GemInfo;
        private var _gemInfo2:GemInfo;
        private var _gemInfo3:GemInfo;
        private var _gemInfo4:GemInfo;
        private var _gemInfo5:GemInfo;
        private var _gemInfo6:GemInfo;
        private var _equipName:GTextFiled;
        private var _gemPackSpr:GemPackListSpr;
        private var _autoBuyBox:GCheckBox;
        private var _combatImg:GImageBitmap;
        private var _equipCombat:BitmapNumberText;
        private var _gemTotalCombat:BitmapNumberText;
        private var _gemInfoArr:Array;
        private var _timeOutIndex:int;
        public static var currSelGem:GemInfo;

        public function GemStrengthenPanel3(param1:Window = null)
        {
            this._gemInfoArr = [];
            super(param1);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.pushUIToDisposeVec(UIFactory.gBitmap(ImagesConst.TextBg3, 157 - 25, 1, this));
            this._equipName = UIFactory.gTextField("", 128, 7, 234, 20, this, GlobalStyle.textFormatHuang.center());
            _currOperEquip = UICompomentPool.getUICompoment(PlayerEquipItem);
            _currOperEquip.setItemStyle(ItemStyleConst.Big, "", 0, 0);
            _currOperEquip.x = 213 + 6;
            _currOperEquip.y = 74 + 24 + 29;
            this.addChild(_currOperEquip);
            _currOperEquip.isShowArroundEffect = false;
            _currOperEquip.configEventListener(MouseEvent.CLICK, onEquipClickHandler);
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GemAllCombat, 6, 55 - 40 - 10, this));
            this._gemTotalCombat = UIFactory.bitmapNumberText(10 + 55, 56 - 23 - 10, "GuildWarResultAddNum.png", 13, 21, -5, this, 13, BitmapNumberText.Mid);
            this._gemInfo1 = UICompomentPool.getUICompoment(GemInfo);
            this._gemInfo1.x = 96 + 30;
            this._gemInfo1.y = 213 - 40 + 20;
            this._gemInfo1.index = 1;
            this.addChild(this._gemInfo1);
            this._gemInfoArr.push(this._gemInfo1);
            this._gemInfo2 = UICompomentPool.getUICompoment(GemInfo);
            this._gemInfo2.x = 348 - 30;
            this._gemInfo2.y = 213 - 40 + 20;
            this._gemInfo2.index = 2;
            this.addChild(this._gemInfo2);
            this._gemInfoArr.push(this._gemInfo2);
            this._gemInfo3 = UICompomentPool.getUICompoment(GemInfo);
            this._gemInfo3.x = 60 + 3 + 26;
            this._gemInfo3.y = 114 + 10 - 19;
            this._gemInfo3.index = 3;
            this.addChild(this._gemInfo3);
            this._gemInfoArr.push(this._gemInfo3);
            this._gemInfo4 = UICompomentPool.getUICompoment(GemInfo);
            this._gemInfo4.x = 381 - 26;
            this._gemInfo4.y = 114 + 10 - 19;
            this._gemInfo4.index = 4;
            this.addChild(this._gemInfo4);
            this._gemInfoArr.push(this._gemInfo4);
            this._gemInfo5 = UICompomentPool.getUICompoment(GemInfo);
            this._gemInfo5.x = 96 + 30;
            this._gemInfo5.y = 38;
            this._gemInfo5.index = 5;
            this.addChild(this._gemInfo5);
            this._gemInfoArr.push(this._gemInfo5);
            this._gemInfo6 = UICompomentPool.getUICompoment(GemInfo);
            this._gemInfo6.x = 348 - 30;
            this._gemInfo6.y = 38;
            this._gemInfo6.index = 6;
            this.addChild(this._gemInfo6);
            this._gemInfoArr.push(this._gemInfo6);
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GemStreng_SelEquipTip, 469, 28, this));
            this._combatImg = UIFactory.gImageBitmap(ImagesConst.Forging_Text_EquipCombat, 191 + 4, 206 - 3, this);
            this._combatImg.visible = false;
            this._equipCombat = UIFactory.bitmapNumberText(264 - 16, 206 - 2, "FightInfoNum.png", 12, 15, -1, this);
            this._gemPackSpr = UICompomentPool.getUICompoment(GemPackListSpr);
            this._gemPackSpr.x = 62 - 20 + 10 + 6;
            this._gemPackSpr.y = 250 + 20 + 46;
            this.addChild(this._gemPackSpr);
            this._autoBuyBox = UIFactory.checkBox(Language.getString(21488), 356 - 93 + 62 + 16, 402 + 5 - 2, 140, 28, this);
            this._autoBuyBox.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this.setGridLayout();
            this.initAutoBuyState();
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.ForgingGemInfoUpdate, this.onGemInfoUpdateHandler);
            Dispatcher.addEventListener(EventName.ForgingOnSelGem, this.onSelGemHandler);
            NetDispatcher.addCmdListener(ServerCommand.RoleLevelUpdate, this.onLevelUpdateHandler);
            Dispatcher.addEventListener(EventName.ForgingEmbedGem, this.onEmbedGemHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.backPackItemUpdateHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.ForgingGemInfoUpdate, this.onGemInfoUpdateHandler);
            Dispatcher.removeEventListener(EventName.ForgingOnSelGem, this.onSelGemHandler);
            NetDispatcher.removeCmdListener(ServerCommand.RoleLevelUpdate, this.onLevelUpdateHandler);
            Dispatcher.removeEventListener(EventName.ForgingEmbedGem, this.onEmbedGemHandler);
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.backPackItemUpdateHandler);
            return;
        }// end function

        override public function updateUI() : void
        {
            var _loc_1:* = GemUtil.getDefaultEquip();
            Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingSelDefaultEquip, _loc_1));
            this.updateGemTotalCombat();
            return;
        }// end function

        private function initAutoBuyState() : void
        {
            if (ClientSetting.local.getIsDone(IsDoneType.AutoBuyGemProp))
            {
                this._autoBuyBox.selected = true;
            }
            else
            {
                this._autoBuyBox.selected = false;
            }
            return;
        }// end function

        override public function addForgingEquip(param1:PlayerEquipItem) : void
        {
            var _loc_2:Array = null;
            var _loc_3:int = 0;
            var _loc_4:SJewelHoleInfo = null;
            this.clearUI();
            super.addForgingEquip(param1);
            if (param1 && param1.itemData)
            {
                this.updateEquipNameInfo();
                this.updateEquipCombat();
                _loc_2 = Cache.instance.forging.getGemDataByEquipType(param1.itemData.itemInfo.type);
                if (_loc_2)
                {
                    _loc_3 = 0;
                    while (_loc_3 < _loc_2.length)
                    {
                        
                        _loc_4 = _loc_2[_loc_3] as SJewelHoleInfo;
                        this._gemInfoArr[_loc_3].data = GemUtil.getGemInfoData(_loc_4);
                        _loc_3++;
                    }
                    this.defaultSelFasterUpgradeGem();
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.ForgingGemSwitchEquip));
            }
            return;
        }// end function

        private function defaultSelFasterUpgradeGem() : void
        {
            return;
        }// end function

        private function setGridLayout() : void
        {
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            if (_loc_1 < 100)
            {
                this._gemInfo5.visible = false;
                this._gemInfo6.visible = false;
                this._gemInfo3.x = 96 + 30;
                this._gemInfo3.y = 38;
                this._gemInfo4.x = 348 - 30;
                this._gemInfo4.y = 38;
            }
            else
            {
                this._gemInfo5.visible = true;
                this._gemInfo6.visible = true;
                this._gemInfo3.x = 60 + 3 + 26;
                this._gemInfo3.y = 114 + 10 - 19 + 10;
                this._gemInfo4.x = 381 - 26;
                this._gemInfo4.y = 114 + 10 - 19 + 10;
            }
            return;
        }// end function

        private function onLevelUpdateHandler(param1:Object) : void
        {
            this.setGridLayout();
            return;
        }// end function

        private function updateEquipNameInfo() : void
        {
            this._equipName.htmlText = ItemsUtil.getItemWholeName(_currOperEquip.itemData, true, 1, false);
            return;
        }// end function

        private function updateBestReview() : void
        {
            var _loc_2:int = 0;
            var _loc_3:ItemData = null;
            var _loc_4:int = 0;
            var _loc_5:String = null;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_1:* = GemStrengthenPanel3.currSelGem;
            if (_loc_1)
            {
                _loc_2 = JewelUpdateConfig.instance.getMaxLevelByGemColor(_loc_1.itemData.itemInfo.color);
                _loc_3 = new ItemData(_loc_1.data.gemData.itemCode, true);
                _loc_4 = _loc_3.itemInfo.type;
                _loc_5 = GameDefConfig.instance.getItem("FightRoleAttribute", _loc_4).text;
                _loc_5 = _loc_5 == "maxLife" ? ("life") : (_loc_5);
                _loc_6 = _loc_3.itemInfo.color;
                _loc_7 = _loc_3.itemInfo.effect;
                _loc_8 = JewelUpdateConfig.instance.getGemAttr(_loc_6, _loc_2, _loc_5);
                _loc_9 = _loc_7 + _loc_8;
                _loc_3.itemInfo.effectEx = _loc_9;
                _loc_3.itemInfo.itemLevel = _loc_2;
            }
            return;
        }// end function

        private function updateGemTotalCombat() : void
        {
            var _loc_1:int = 0;
            var _loc_3:ItemData = null;
            var _loc_4:Array = null;
            var _loc_5:SJewelHoleInfo = null;
            var _loc_6:GemInfoData = null;
            var _loc_2:* = Cache.instance.pack.packRolePackCache.getAllRoleEquips();
            for each (_loc_3 in _loc_2)
            {
                
                _loc_4 = Cache.instance.forging.getGemDataByEquipType(_loc_3.itemInfo.type);
                for each (_loc_5 in _loc_4)
                {
                    
                    _loc_6 = GemUtil.getGemInfoData(_loc_5, _loc_3);
                    if (_loc_6.gemData && _loc_6.operType != GemOperType.OperType_CanBuy)
                    {
                        _loc_1 = _loc_1 + EquipmentUtil.getGemCombat(_loc_6.gemData);
                    }
                }
            }
            this._gemTotalCombat.text = _loc_1 > 0 ? (_loc_1 + "") : ("");
            return;
        }// end function

        private function onSelGemHandler(event:DataEvent) : void
        {
            var _loc_3:GemInfo = null;
            var _loc_4:GemInfo = null;
            var _loc_2:* = event.data as int;
            for each (_loc_4 in this._gemInfoArr)
            {
                
                if (_loc_4.index == _loc_2)
                {
                    _loc_3 = _loc_4;
                    break;
                }
            }
            if (GemStrengthenPanel3.currSelGem == _loc_3)
            {
                return;
            }
            if (GemStrengthenPanel3.currSelGem)
            {
                GemStrengthenPanel3.currSelGem.isSelected = false;
            }
            _loc_3.isSelected = true;
            GemStrengthenPanel3.currSelGem = _loc_3;
            return;
        }// end function

        private function onGemInfoUpdateHandler(param1:Object) : void
        {
            var _loc_3:PlayerEquipItem = null;
            var _loc_2:* = param1 as SJewelHolesInfoMsg;
            if (_loc_2)
            {
                _loc_3 = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
                if (!(_loc_3 && _loc_3.itemData))
                {
                    return;
                }
                this._timeOutIndex = setTimeout(this.gemInfoUpdate, 200);
            }
            return;
        }// end function

        private function gemInfoUpdate() : void
        {
            var _loc_3:int = 0;
            var _loc_4:SJewelHoleInfo = null;
            clearTimeout(this._timeOutIndex);
            var _loc_1:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (!(_loc_1 && _loc_1.itemData))
            {
                return;
            }
            var _loc_2:* = Cache.instance.forging.getGemDataByEquipType(_loc_1.itemData.itemInfo.type);
            if (_loc_2)
            {
                _loc_3 = 0;
                while (_loc_3 < _loc_2.length)
                {
                    
                    _loc_4 = _loc_2[_loc_3] as SJewelHoleInfo;
                    this._gemInfoArr[_loc_3].data = GemUtil.getGemInfoData(_loc_4);
                    _loc_3++;
                }
            }
            this.updateEquipCombat();
            this.updateGemTotalCombat();
            return;
        }// end function

        private function backPackItemUpdateHandler(param1:Object) : void
        {
            var _loc_2:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (!(_loc_2 && _loc_2.itemData))
            {
                return;
            }
            this.gemInfoUpdate();
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            if (event.target == this._autoBuyBox)
            {
                if (this._autoBuyBox.selected == true)
                {
                    ClientSetting.local.setIsDone(true, IsDoneType.AutoBuyGemProp);
                }
                else
                {
                    ClientSetting.local.setIsDone(false, IsDoneType.AutoBuyGemProp);
                }
            }
            return;
        }// end function

        private function onEmbedGemHandler(event:DataEvent) : void
        {
            var _loc_4:EquipGemOperState = null;
            var _loc_5:Array = null;
            var _loc_6:SJewelHoleInfo = null;
            var _loc_7:Array = null;
            var _loc_8:int = 0;
            var _loc_9:ItemData = null;
            var _loc_10:EquipGemOperState = null;
            var _loc_11:PlayerEquipItem = null;
            var _loc_2:* = event.data as ItemData;
            var _loc_3:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (_loc_3.itemData)
            {
                _loc_4 = GemUtil.getEquipGemOperState(_loc_3.itemData);
                if (_loc_4.isCanEmbed)
                {
                    _loc_5 = Cache.instance.forging.getGemDataByEquipType(_loc_3.itemData.itemInfo.type);
                    for each (_loc_6 in _loc_5)
                    {
                        
                        if (_loc_6.itemCode == 0)
                        {
                            GameProxy.equip.jewelEmbedNew(_loc_3.itemData.uid, _loc_6.index, _loc_2.uid);
                            GemUtil.currOperType = GemOperType.OperType_CanEmbed;
                            return;
                        }
                    }
                }
                else
                {
                    _loc_7 = EquipJewelMatchConfig.instance.getEquipTypeByJewelType(_loc_2.itemInfo.type);
                    if (_loc_7.length == 2)
                    {
                        _loc_8 = _loc_3.itemData.itemInfo.type == _loc_7[0] ? (_loc_7[1]) : (_loc_7[0]);
                        _loc_9 = Cache.instance.pack.packRolePackCache.getEquipByType(_loc_8);
                        if (_loc_9)
                        {
                            _loc_10 = GemUtil.getEquipGemOperState(_loc_9);
                            if (_loc_10.isCanEmbed)
                            {
                                _loc_3.isSelect = false;
                                _loc_11 = (GameController.forging.view as ForgingModule).equipDisplaySpr.getEquipByType(_loc_8);
                                (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip = _loc_11;
                                _loc_11.setSelEffect(true);
                                Dispatcher.dispatchEvent(new DataEvent(EventName.AddForgingEquip, _loc_11));
                            }
                        }
                    }
                }
            }
            return;
        }// end function

        private function updateEquipCombat() : void
        {
            var _loc_1:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (_loc_1.itemData)
            {
                this._equipCombat.text = EquipmentUtil.getEquipCombat(_loc_1.itemData) + "";
                this._combatImg.visible = true;
            }
            return;
        }// end function

        override public function clearUI() : void
        {
            var _loc_1:GemInfo = null;
            super.clearUI();
            for each (_loc_1 in this._gemInfoArr)
            {
                
                _loc_1.clear();
            }
            GemStrengthenPanel3.currSelGem = null;
            this._equipName.text = "";
            this._equipCombat.text = "";
            this._combatImg.visible = false;
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._equipName.dispose();
            this._equipName = null;
            _currOperEquip.dispose();
            _currOperEquip = null;
            this._gemInfo1.dispose();
            this._gemInfo1 = null;
            this._gemInfo2.dispose();
            this._gemInfo2 = null;
            this._gemInfo3.dispose();
            this._gemInfo3 = null;
            this._gemInfo4.dispose();
            this._gemInfo4 = null;
            this._gemInfo5.dispose();
            this._gemInfo5 = null;
            this._gemInfo6.dispose();
            this._gemInfo6 = null;
            this._gemPackSpr.dispose();
            this._gemPackSpr = null;
            this._autoBuyBox.dispose();
            this._autoBuyBox = null;
            this._combatImg.dispose();
            this._combatImg = null;
            this._equipCombat.dispose();
            this._equipCombat = null;
            this._gemTotalCombat.dispose();
            this._gemTotalCombat = null;
            this._gemInfoArr.length = 0;
            this.removeListeners();
            if (BuyGiftItemWin.isViewShow)
            {
                BuyGiftItemWin.instance.hide();
            }
            return;
        }// end function

    }
}
